package com.dhnsoft.代码随想录.字符串;

/**
 * ClassName: d04_151_反转字符串中的单词
 * Package: com.dhnsoft.代码随想录.字符串
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/19 9:15
 * @Version 1.0
 */
public class d04_151_反转字符串中的单词 {

    public static void main(String[] args) {
        String the_sky_is_blue = reverseWords("a good   example");
        System.out.println(the_sky_is_blue);
    }

    public static String reverseWords(String s) {
        StringBuilder sb1 = new StringBuilder();
        char[] word = s.toCharArray();
        for (int i=word.length-1;i>=0;i--){
            if(word[i]==' '){
                continue;
            }
            StringBuilder sb2 = new StringBuilder();
            while (i>=0&&word[i]!=' '){
                sb2.append(word[i]);
                i--;
            }
            sb1.append(sb2.reverse());
            sb1.append(' ');
        }
        sb1.deleteCharAt(sb1.length()-1);
        return sb1.toString();
    }



}
